import store from '../store';
import { RoundVo, DicAreaVo } from '../api/Interface';
import moment from 'moment';
// tslint:disable-next-line:no-any
const state: any = store.state;
const filters = {
    // 通用csConst过滤器
    constFilter: (data, constType) => {
        const tempObj: { name: string}[] = state.csConst[constType].filter((item) => {
            return data === item.id;
        });
        if (tempObj.length === 0) {
            return '';
        } else {
            return tempObj[0].name;
        }
    },
    // 获取根据id 获取 dic字典中的name
    getDicNameById: (id: string, type: string) => {
        const localDict = state.user.dictionary.dicList;
        let name: string | null = '';
        if (!id) {
            return null;
        }
        function list(arr: object[]) {
            if (arr.length === 0) {
                return null;
            }
            arr.map((item: {dictValue: string; dictName: string; children: object[]}) => {
                if (String(id) === String(item.dictValue)) {
                    name = item.dictName;
                } else if (item.children && item.children.length > 0) {
                    name = list(item.children);
                }
            });
            return name;
        }
        return list(localDict[type]);
    },
    // 获取根据id 获取 轮次 字典中的name
    getRoundNameById: (id: string|number) => {
        const localDict: RoundVo[] = state.user.dictionary.roundList;
        let name: string | null = '';
        if (!id) {
            return null;
        }
        function list(arr: RoundVo[]) {
            if (arr.length === 0) {
                return null;
            }
            arr.map((item: RoundVo) => {
                if (String(id) === String(item.id)) {
                    name = item.roundName;
                } else if (item.children && item.children.length > 0) {
                    name = list(item.children);
                }
            });
            return name;
        }
        return list(localDict);
    },
    // 获取根据id 获取 地区 字典中的name
    getAreaNameById: (id: string|number, key: string = 'cnName') => {
        const localDict: DicAreaVo[] = state.user.dictionary.dicAreaList;
        let name: string | null = '';
        if (!id) {
            return null;
        }
        function list(arr: DicAreaVo[]) {
            if (arr.length === 0) {
                return null;
            }
            arr.map((item: DicAreaVo) => {
                if (String(id) === String(item.id)) {
                    name = item[key];
                } else if (item.children && item.children.length > 0) {
                    name = list(item.children);
                }
            });
            return name;
        }
        return list(localDict);
    },
    // 根据id显示字典中对应的name
    getNameById: (id: string, type: string, key?: string) => {
        if (!id) {
            return null;
        }
        let name;
        if (type === 'roundList') { // 轮次
            name = filters.getRoundNameById(id);
        } else if (type === 'area') {
            name = filters.getAreaNameById(id, key);
        } else { // dic
            name = filters.getDicNameById(id, type);
        }
        return name;
    },
    cmsMoment: (data: string | number, format: string = 'YYYY-MM-DD') => {
        if (data) {
            return moment(data).format(format);
        } else {
            return null;
        } 
    }
};
export default filters;